// index.js
var liveroom = require('../../utils/liveroom.js');
var getlogininfo = require('../../getlogininfo.js');
var app = getApp()

Page({
  data: {
    roomName: '',	// 房间名称
    userName: '',	// 用户名称
    roomList: [],	// 房间列表
    isGetLoginInfo: false,  // 是否已经获取登录信息
    firstshow: true,// 第一次显示页面
    tapTime: '',		// 防止两次点击操作间隔太快
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 获取登录信息
    var self = this;
    getlogininfo.getLoginInfo({
      type: 'live_room',
      success: function (ret) {
        self.data.firstshow = false;
        self.data.isGetLoginInfo = true;
        self.getRoomList();
        // console.log('我的昵称：',ret.userName);
        console.log('我的昵称：' + ret.userName);
        self.setData({
          userName: ret.userName
        });
      },
      fail: function (ret) {
        self.data.isGetLoginInfo = false;
        wx.showModal({
          title: '获取登录信息失败',
          content: ret.errMsg,
          showCancel: false,
          complete: function () {
            wx.navigateBack({});
          }
        });
      }

    });
  },
  onLoad: function (options) {
    app.login({
      share_id: options.share_id
    })
    var thiz = this
    var PageInterface = app.globalData.domain + app.globalData.PageInterface
    var domain = app.globalData.domain
    wx.getSystemInfo({
      success: function (res) {
        thiz.setData({
          domain: domain,
          hiddenLoading: false,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        });
      }
    })
    //首页轮播图
    wx.request({
      url: PageInterface,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        format_type: 'json',
        api_cate: 'Banner',
        api: 1001,
        c_id: 0
      },
      success: function (res) {
        if (res.data.data.length > 0) {
          res.data.data.forEach(function (element, index) {
            element.img_url = domain + element.img_url
          })
          thiz.setData({
            banners: res.data.data
          })
        }
      }
    });
    //热马火热商品
    wx.request({
      url: PageInterface,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        format_type: 'json',
        api_cate: 'Goods',
        api: 4002,
        goods: 'home',
        limit: 4
      },
      success: function (res) {
        if (res.data.data.length > 0) {
          res.data.data.forEach(function (element, index) {
            element.default_img = domain + element.default_img
          })
          thiz.setData({
            shangpins: res.data.data
          })
        }
      }
    });
    //热马最新导购
    wx.request({
      url: PageInterface,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        format_type: 'json',
        api_cate: 'Curr',
        api: 3002,
        curr: 'home',
        limit: 4
      },
      success: function (res) {
        if (res.data.data.length > 0) {
          res.data.data.forEach(function (element, index) {
            element.curr_img = domain + element.curr_img
          })
          thiz.setData({
            zhibos: res.data.data
          })
        }
      }
    });
    //热马推荐商户
    wx.request({
      url: PageInterface,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        format_type: 'json',
        api_cate: 'Company',
        api: 6001,
        company: 'home',
        limit: 5
      },
      success: function (res) {
        if (res.data.data.length > 0) {
          res.data.data.forEach(function (element, index) {
            element.company_logo = domain + element.company_logo
          })
          thiz.setData({
            companys: res.data.data,
            hiddenLoading: true
          })
        }
      }
    });
  },
  onShow:function(){
    // 页面显示就拉取一次房间列表
		console.log('roomlist onshow');
    !this.data.firstshow && this.getRoomList(function () { });

    //热马最新导购
    var thiz = this;
    var PageInterface = app.globalData.domain + app.globalData.PageInterface  ;
    var domain = app.globalData.domain;
    wx.request({
      url: PageInterface,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        format_type: 'json',
        api_cate: 'Curr',
        api: 3002,
        curr: 'home',
        limit: 4
      },
      success: function (res) {
        if (res.data.data.length > 0) {
          res.data.data.forEach(function (element, index) {
            element.curr_img = domain + element.curr_img
          })
          thiz.setData({
            zhibos: res.data.data
          })
        }
      }
    });
  },
  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function () {
    this.getRoomList();
    wx.stopPullDownRefresh();
  },
  /**
   * 分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '热马品牌商城小程序',
      path: '/pages/index/index?share_id=' + wx.getStorageSync('user_id'),
      imageUrl: "/pages/images/logo.png",
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '转发成功！',
          icon: 'success',
          duration: 2000
        })
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  /**
	 * 搜索框操作
	 */
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  /**
	 * 拉取房间列表
	 * @return {[type]}            [description]
	 */
	getRoomList: function () {
    var self = this;
    console.log(!self.data.isGetLoginInfo)
    if (!self.data.isGetLoginInfo) {
      wx.showModal({
        title: '提示',
        content: '登录信息初始化中，请稍后再试',
        showCancel: false
      })
      return;
    }
    liveroom.getRoomList({
      data: {
        index: 0,
        cnt: 20
      },
      success: function (ret) {
        console.log(ret.rooms)
        self.setData({
          roomList: ret.rooms
        });
        console.log('拉取房间列表成功');
      },
      fail: function (ret) {
        console.log(ret);
        wx.showModal({
          title: '拉取房间列表失败',
          content: ret.errMsg,
          showCancel: false
        });
      }
    });
  },

  /**
	 * 进入liveroom页面
	 * @param  {[type]} e [description]
	 * @return {[type]}   [description]
	 */
  goRoom: function (e) {
    // 防止两次点击操作间隔太快
    var nowTime = new Date();
    if (nowTime - this.data.tapTime < 1000) {
      return;
    }
    var url = '../liveroom/room/room?roomID=' + e.currentTarget.dataset.roomid + '&roomName=' + e.currentTarget.dataset.roomname + '&userName=' +      this.data.userName + '&accelerateURL=' + e.currentTarget.dataset.accelerateurl;
    wx.navigateTo({ url: url });
    wx.showToast({
      title: '进入房间',
      icon: 'success',
      duration: 1000
    })
    this.setData({ 'tapTime': nowTime });
  }
})